package appapi.service.impl;

import appapi.Request.AccessPageReq;
import appapi.Vo.brider.AccessVO;
import appapi.Vo.brider.AddAccess;
import appapi.common.ResponseBean;
import appapi.dao.ProjectMapper;
import appapi.entity.Zhongjiao.ProjectDto;
import appapi.entity.basic.UserInfo;
import appapi.entity.bridge.AccessAuthInfo;
import appapi.entity.bridge.AccessControl;
import appapi.mapper.AccessAuthMapper;
import appapi.mapper.AccessMapper;
import appapi.service.AccessService;
import appapi.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author hlm
 * @date
 * @description: 门禁设备
 */
@Service
public class AccessServiceImpl extends ServiceImpl<AccessMapper, AccessControl> implements AccessService {

   @Resource
   private AccessMapper accessMapper;

   @Resource
   private ProjectMapper projectMapper;

   @Resource
   private AccessAuthMapper accessAuthMapper;

    /**
     * 分页查询门禁设备列表
     *
     * @return
     */
    @Override
    public PageInfo<AccessVO> getAccessByPage(AccessPageReq req) {
        PageInfo<AccessVO> pageInfo = PageHelper.startPage(req.getPageNum(), req.getPageSize())
                .doSelectPageInfo(() -> accessMapper.selectAccessListByPage(req));
        return pageInfo;
    }

    /**
     * 新增
     *
     * @return
     */
    @Override
    public ResponseBean insertAccess(AddAccess addAccess , UserInfo userInfo) {
        LambdaQueryWrapper<AccessControl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccessControl::getProjId , addAccess.getProjId());
        wrapper.eq(AccessControl::getAcceName , addAccess.getAcceName());
        wrapper.eq(AccessControl::getAcceNo , addAccess.getAcceNo());
        int count = accessMapper.selectCount(wrapper);
        if(count > 0){
            return ResponseBean.fail("该门禁已存在");
        }
        AccessControl accessControl = BeanCopyUtils.copyBean(addAccess , AccessControl.class);
        accessControl.setCreateTime(LocalDateTime.now());
        accessControl.setCreateUser(userInfo.getUserid());
        accessControl.setLastModifyUser(userInfo.getUserid());
        accessControl.setLastModifyTime(LocalDateTime.now());
        accessMapper.insert(accessControl);
        return ResponseBean.success();
    }


    /**
     * 删除
     *
     * @return
     */
    @Override
    public void delAccess(Long acceId) {
        accessMapper.deleteById(acceId);
        //删除对应的门禁授权人员
        AccessAuthInfo accessAuthInfo = new AccessAuthInfo();
        accessAuthInfo.setAuthFlag(0);
        LambdaQueryWrapper<AccessAuthInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccessAuthInfo::getAccessId , acceId);
        accessAuthMapper.update(accessAuthInfo , wrapper);
    }


    /**
     * 详情
     *
     * @return
     */
    @Override
    public ResponseBean getAccessModel(Long acceId) {
        AccessControl accessControl = accessMapper.selectById(acceId);
        AccessVO accessVO = BeanCopyUtils.copyBean(accessControl , AccessVO.class);
        //封装项目信息
        ProjectDto projectDto = projectMapper.selectByPrimaryKey(Long.valueOf(accessVO.getProjId()));
        if (projectDto != null) {
            accessVO.setProjName(projectDto.getProjName());
        }
        return ResponseBean.success(accessVO);
    }
}
